﻿using com.winter.manager;
using UnityEngine;
using UnityEngine.UI;

namespace com.winter.view._02_game
{
    public class PausePanel : MonoBehaviour
    {
        [Header("打开暂定界面的按钮")] public Button btnPause;
        [Header("继续游戏的按钮")] public Button btnPlay;
        [Header("回到菜单的按钮")] public Button btnHome;

        private Animator animator;

        private static readonly int Speed = Animator.StringToHash("Speed");

        // Start is called before the first frame update
        public void Start()
        {
            initVariable();

            initListener();

            initView();
        }

        private void initView()
        {
            // 一开始可以点击
            btnPause.interactable = true;
            // 本界面显示方便执行动画
            gameObject.SetActive(true);
        }

        private void initVariable()
        {
            animator = GetComponent<Animator>();

            if (btnPlay == null)
            {
                btnPlay = transform.Find("btn_menu/btn_play").GetComponent<Button>();
            }

            if (btnHome == null)
            {
                btnHome = transform.Find("btn_menu/btn_home").GetComponent<Button>();
            }
        }

        private void initListener()
        {
            btnPause.onClick.AddListener(pauseFuc);
            btnPlay.onClick.AddListener(playFuc);
            btnHome.onClick.AddListener(menuFuc);
        }

        private void playFuc()
        {
            // change game state (running)
            Time.timeScale = 1;
            GameManager.getInstance().gameState = GameManager.game_STATE.running;

            // check env
            float speed = animator.GetFloat(Speed);
            if (speed.Equals(1f))
            {
                Debug.LogError("change");
                animator.SetFloat(Speed, -1f);
            }

            // change UI state
            btnPause.interactable = true;
            btnPlay.interactable = false;

            // change animator state (hide pause panel)
            animator.enabled = true;
        }

        static void menuFuc()
        {
        }

        private void pauseFuc()
        {
            // check env
            float speed = animator.GetFloat(Speed);
            if (speed.Equals(-1f))
            {
                animator.SetFloat(Speed, 1f);
            }

            // change ui state
            btnPlay.interactable = true;
            btnPause.interactable = false;

            // change animator state (show pause panel)
            animator.enabled = true;


            // change game state
            GameManager.getInstance().gameState = GameManager.game_STATE.pause;
        }

        private void resume()
        {
            // change animator state
            animator.enabled = false;
            animator.SetFloat(Speed, -1f);

            // change game state (pause)
            Time.timeScale = 0;
        }

        /// <summary>
        /// pause 动画第一帧触发，将播放速度改为1
        /// 方便下次点击播放动画
        /// </summary>
        private void initSpeed()
        {
            // change animator state
            animator.enabled = false;
            animator.SetFloat(Speed, 1f);
        }
    }
}